package com.example.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@AllArgsConstructor
public enum CurrencyEnum {

    cny("CNY","人民币"),

    usd("USD","美元");

    @Getter
    private String code;

    @Getter
    private String desc;

    /**
     * 获取所有的枚举名称
     * @return
     */
    public static List<String> getCurrencyName(){
        return Stream.of(values()).map(Enum::name).collect(Collectors.toList());
    }

    /**
     * 获取所有code字段的值
     * @return
     */
    public static List<String> getFieldValues(){
        return Stream.of(values()).map(e->e.code).collect(Collectors.toList());
    }

    /**
     * 根据code查询枚举值
     * @param code
     * @return
     */
    public static CurrencyEnum getEnumByCode(String code){
        return Stream.of(values()).filter(e-> Objects.equals(e.code,code)).findFirst().orElse(null);
    }

    /**
     * 根据code获取描述desc信息
     * @param code
     * @return
     */
    public static String getDescByCode(String code){
        return Stream.of(values()).filter(e->Objects.equals(e.code,code)).findFirst().map(CurrencyEnum::getDesc).orElse(null);
    }

    /**
     * 获取code与枚举的映射Map
     * @return
     */
    public static Map<String, CurrencyEnum> getCurrencyMap(){
        return Stream.of(values()).collect(Collectors.toMap(Enum::name, Function.identity(),(x, y)->x));
    }

    /**
     * 获取code与desc的映射Map
     * @return
     */
    public static Map<String,String> getCodeDescMap(){
        return Stream.of(values()).collect(Collectors.toMap(Enum::name, CurrencyEnum::getDesc,(x, y)->x));
    }

    /**
     * 判断状态是否合法
     * @param code
     * @return
     */
    public static boolean isValid(String code){
        return Stream.of(values()).anyMatch(e->Objects.equals(e.code,code));
    }


}
